package com.zty.xiaomi.server.Listener;

import com.zty.xiaomi.server.Service.Order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private OrderService orderService;
    @Value("${order.Canceltheorder}")
    private String Tobepaid;
    @Value("${order.Automaticreceiving}")
    private String Automaticreceiving;
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        super.onMessage(message, pattern);
        String key = message.toString();
        String orderNumber = null;
        if (key.contains(Tobepaid)) {
           orderNumber = key.replace(Tobepaid, "");
            orderService.UpdateOrder(5, "已取消", Integer.parseInt(orderNumber));
            return;
        }
        orderNumber = key.replace(Automaticreceiving, "");
        orderService.UpdateOrder(4, "交易成功", Integer.parseInt(orderNumber));
    }
}
